package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.DevelopmentTop;
import com.ruoyi.system.mapper.SysDeptMapper;
import io.lettuce.core.dynamic.annotation.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.PopularProcessMapper;
import com.ruoyi.system.domain.PopularProcess;
import com.ruoyi.system.service.IPopularProcessService;

/**
 * 小区人气和过程管控Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-05
 */
@Service
public class PopularProcessServiceImpl implements IPopularProcessService 
{
    private static final Logger log = LoggerFactory.getLogger(PopularProcess.class);
    @Autowired
    private PopularProcessMapper popularProcessMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;

    /**
     * 查询小区人气和过程管控
     * 
     * @param id 小区人气和过程管控主键
     * @return 小区人气和过程管控
     */
    @Override
    public PopularProcess selectPopularProcessById(Long id)
    {
        return popularProcessMapper.selectPopularProcessById(id);
    }

    /**
     * 查询小区人气和过程管控列表
     * 
     * @param popularProcess 小区人气和过程管控
     * @return 小区人气和过程管控
     */
    @Override
    public List<PopularProcess> selectPopularProcessList(PopularProcess popularProcess)
    {
        return popularProcessMapper.selectPopularProcessList(popularProcess);
    }

    @Override
    public List<PopularProcess> selectPopularProcessListNew(PopularProcess popularProcess)
    {
        return popularProcessMapper.selectPopularProcessListNew(popularProcess);
    }

    /**
     * 新增小区人气和过程管控
     * 
     * @param popularProcess 小区人气和过程管控
     * @return 结果
     */
    @Override
    public int insertPopularProcess(PopularProcess popularProcess)
    {
        return popularProcessMapper.insertPopularProcess(popularProcess);
    }

    /**
     * 修改小区人气和过程管控
     * 
     * @param popularProcess 小区人气和过程管控
     * @return 结果
     */
    @Override
    public int updatePopularProcess(PopularProcess popularProcess)
    {
        return popularProcessMapper.updatePopularProcess(popularProcess);
    }

    /**
     * 批量删除小区人气和过程管控
     * 
     * @param ids 需要删除的小区人气和过程管控主键
     * @return 结果
     */
    @Override
    public int deletePopularProcessByIds(Long[] ids)
    {
        return popularProcessMapper.deletePopularProcessByIds(ids);
    }

    /**
     * 删除小区人气和过程管控信息
     * 
     * @param id 小区人气和过程管控主键
     * @return 结果
     */
    @Override
    public int deletePopularProcessById(Long id)
    {
        return popularProcessMapper.deletePopularProcessById(id);
    }

    @Override
    public List<PopularProcess> selectPopularProcessListTable(Long deptId,String targetTime)
    {
        return popularProcessMapper.selectPopularProcessListTable(deptId,targetTime);
    }

    /**
     * 导入数据
     */
    @Override
    public String importPopularProcess(List<PopularProcess> popularProcessList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(popularProcessList) || popularProcessList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (PopularProcess popularProcess : popularProcessList)
        {
            try
            {
                // 验证是否存在这个部门
                SysDept sysDepts = sysDeptMapper.selectDeptOnName(popularProcess.getDeptName());
                if (StringUtils.isNotNull(sysDepts))
                {
                    popularProcess.setCreateBy(operName);
                    popularProcess.setDeptId(sysDepts.getDeptId());
                    popularProcessMapper.insertPopularProcess(popularProcess);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、小区 " + popularProcess.getDeptName() + " 导入成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、小区 " + popularProcess.getDeptName() + " 不存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、业务员 " + popularProcess.getDeptName()  + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

}
